#include "RingQueue.hpp"
#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include "Task.hpp"
void *productor(void *args)
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(args);
    while (true)
    {
        int x=rand()%100;
        int y=rand()%100;
        char op=opers[rand()%opers.size()];
        Task t(x,y,op);
        rq->Push(t);
        std::cout << "product : " << t.PrintTask() << std::endl;
        sleep(1);
    }
    return nullptr;
}

void *comsumer(void *args)
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(args);
    while(true){
        Task t;
        rq->Pop(&t);
        t();
        std::cout << "comsumer : " << t.PrintResult() << std::endl;
    }
    return nullptr;
}

int main()
{
    srand(time(nullptr)^pthread_self());
    pthread_t c[3], p[2];
    RingQueue<int> *rq = new RingQueue<int>();
    pthread_create(&c[0], nullptr, comsumer, rq);
    pthread_create(&c[1], nullptr, comsumer, rq);
    pthread_create(&c[2], nullptr, comsumer, rq);

    pthread_create(&p[0], nullptr, productor, rq);
    pthread_create(&p[1], nullptr, productor, rq);


    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(c[2], nullptr);

    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);

    return 0;
}